package com.lib.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.lib.model.dto.BookReviewDto;

public class BookReviewDao extends SqlMapClientDaoSupport implements
		IBookReviewDao {
	@Override
	public ArrayList<BookReviewDto> getAllReview(int startPage, int startBlock,
			String book_num) {
		ArrayList<BookReviewDto> reviewList = null;

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startpage", startPage);
		map.put("endpage", startBlock);
		map.put("book_num", book_num);

		reviewList = (ArrayList<BookReviewDto>) getSqlMapClientTemplate()
				.queryForList("BookReview.getAllReview", map);

		return reviewList;
	}

	@Override
	public int getBookReviewCount(String book_num) {
		int count = 0;

		count = (Integer) getSqlMapClientTemplate().queryForObject(
				"BookReview.getAllReviewCount", book_num);

		return count;
	}

	@Override
	public boolean addBookReview(BookReviewDto bDto) {
		int count = 0;

		try {
			getSqlMapClientTemplate().insert("BookReview.addBookReview", bDto);
			count = 1;
		} catch (Exception e) {
			count = -1;
		}
		return count > 0 ? true : false;
	}

	@Override
	public boolean delReview(BookReviewDto bDto) {
		int count = 0;

		try {
			getSqlMapClientTemplate().delete("BookReview.delReview",bDto);
			count = 1;
		} catch (Exception e) {
			count = -1;
		}
		return count>0?true:false;
	}
}
